/** 
 * 用户相关请求模块
 * */ 

import request from "../utils/request";
import store from "../store";

// 登录
const login = function (data) {
    return request({
        method: 'POST',
        url: '/v1_0/authorizations',
        data: data
    })
}

// 发送验证码
const sendSms = mobile => {
    return request({
        method: 'GET',
        url: `/v1_0/sms/codes/${mobile}`
    })
}

// 刷新用户token
const refreshToken = (refreshToken) => {
    return request({
        method: 'PUT',
        url: '/v1_0/authorizations',
        headers: {
            Authorization: `Bearer ${refreshToken}`
        }
    })
}

// 获取用户(头像 名称 粉丝数 获赞数等)
const getUserInfo = () => {
    return request({
        method: 'GET',
        url: '/v1_0/user',
        // 添加请求头数据, 我们已经在axios请求拦截器里配置了
        // headers: {
        //     // 数据格式注意：Bearer后有一个空格
        //     Authorization: `Bearer ${store.state.user.token}`
        // }
    })
}

// 获取用户频道列表
const getUserChannels = () => {
    return request({
        method: 'GET',
        url: '/v1_0/user/channels'
    })
}

// 关注用户
const addFollow = (authorId) => {
    return request({
        method: 'POST',
        url: '/v1_0/user/followings',
        data: {
            target: authorId
        }
    })
}

// 取消关注用户
const deleteFollow = (authorId) => {
    return request({
        method: 'DELETE',
        url: `/v1_0/user/followings/${authorId}`
    })
}

export {login, sendSms, refreshToken, getUserInfo, getUserChannels, addFollow, deleteFollow}